import request from '../../request/index'
import regeneratorRuntime from '../../lib/runtime/runtime';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    height: 0,
    cate: [],
    active_index: 0,
    categories: [],
    childrenList: []
  },

  //获取数据
  async getCate() {
    let res = await request('/categories')
    this.setData({
      categories: res
    })
    let {
      categories
    } = this.data;
    let childrenList = categories[0].children
    this.setData({
      childrenList
    })
    wx.setStorageSync('categories', {
      time: new Date(),
      categories: categories
    });
  },
  //侧边导航栏逻辑
  changHandlerIndex(e) {
    let {
      index
    } = e.currentTarget.dataset;
    this.setData({
      active_index: index,
      childrenList: this.data.categories[index].children
    })
  },
  //数据缓存
  setStorageData() {
    let {
      categories,
      time
    } = wx.getStorageSync('categories');
    console.log(categories);
    //如果没有数据
    if (!categories) {
      this.getCate()
    } else {
      //数据更新周期
      if (new Date() - time > 1000 * 100) {
        this.getCate()
      } else {
        let childrenList = categories[0].children
        this.setData({
          categories,
          childrenList
        })
      }
    }
  },
  //路由跳转
  GoToDetail(e) {
    let {
      cid
    } = e.currentTarget.dataset
    wx.navigateTo({
      url: '/pages/goods_list/index?cid=' + cid,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setStorageData()
    let my_height = wx.getSystemInfoSync().windowHeight
    this.setData({
      height: my_height - 45
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})